# event listener
from direct.showbase import DirectObject
# event listener
from Cube.map import event_QMEMBERLISTMGR_MEMBERJOIN, event_QMEMBERLISTMGR_MEMBERMOVE,\
                     event_QMEMBERLISTMGR_MEMBERLEAVE
from Cube.command.QCommand.EventDefine import event_QCMD_CHATMESSAGE

# gui
from direct.gui.DirectGui import OkDialog
from Cube.gui.DirectGUIPropertiesAccess import BtnCmdSet
from Cube.map import QMemberType
# uiString
from Shell.data.UIString import uiString

class GameLobbyViewHandler(DirectObject.DirectObject):
    def __init__(self):
        self.accept(event_QMEMBERLISTMGR_MEMBERJOIN,self.MemberJoinHandle)
        self.accept(event_QMEMBERLISTMGR_MEMBERLEAVE,self.MemberLeaveHandle)
        self.accept(event_QMEMBERLISTMGR_MEMBERMOVE,self.MemberMoveHandle)
        self.accept(event_QCMD_CHATMESSAGE,self.ChatMessagePrint)

    def ControllerSet(self, controller):
        self.controller = controller
        self.MemberListControlFrame.TeamBtnCallBackSet(controller.MemberMoveHandle)
        self.MemberListControlFrame.OptionCallbackSet(controller.MemberAssignHandle)
        self.ChatRoom.ChatEntryCallBackSet(controller.ChatStringHandle)
        BtnCmdSet(self.btn_HostIP, controller.HostIPDisplayHandle)
        if self.model.IsServerMode():
            BtnCmdSet(self.btn_StartGame, controller.StartGameHandle)
        BtnCmdSet(self.btn_Quit, controller.QuitGameLobbyHandle)

        # after ControllerSet
        self.MemberListControlFrame.Update(self.MemberData)

# host ip display ---------------------------------------------------
    def HostIPDisplayHandle(self, text):
        self.HostIPDisplayDialog = OkDialog(text = text,
                                            command=self.controller.HostIPDisplayDialogDestroy)

    def HostIPDisplayDialogDestroy(self):
        self.HostIPDisplayDialog.destroy()

# messages display --------------------------------------------------
    def MessageDisplay(self, uiString, fn):
        self.MsgDisplayDialog = OkDialog(text = uiString, command = fn)

    def MessageDisplayDialogDestroy(self):
        self.MsgDisplayDialog.destroy()

# member join/leave/move handle -------------------------------------
    def MemberJoinHandle(self, memberName, memType, memberID):
        self.MemberListControlFrame.Update(self.MemberData)
        if memType == QMemberType.HUMAN:
            # display message on chat room's text board
            msgText = "%s%s"%(memberName,uiString["has joined"])
            self.ChatRoom.appendMessage(msgText)

    def MemberLeaveHandle(self, memberID, memberName):
        self.MemberListControlFrame.Update(self.MemberData)
        # display message on chat room's text board
        msgText = "%s%s"%(memberName,uiString["has leaved"])
        self.ChatRoom.appendMessage(msgText)

    def MemberMoveHandle(self, memberID, newMemberID):
        if newMemberID is not None:
            self.MemberListControlFrame.Update(self.MemberData)

# chat message ------------------------------------------------------
    def ChatMessagePrint(self, memberID, message):
        member = self.MemberData.ID2MemberGet(memberID)
        memberName = member.MemberName
        printText = "%s: %s"%(memberName, message)
        self.ChatRoom.appendMessage(printText)
        self.ChatRoom.InputEntryClear()

# destroy -----------------------------------------------------------
    def destroy(self):
        self.ignoreAll()
